<?php


namespace app\shop\model\order;


use app\shop\model\config\TypeDesignModel;
use app\shop\model\user\UserModel;
use Carbon\Carbon;
use think\Model;
use think\model\concern\SoftDelete;

class OrderModel extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;
    protected $name = 'order';
    protected $autoWriteTimestamp = true;

    const WAIT_DISTRIBUTE = 0;
    const UNDERWAY = 1;
    const FINALIZE = 2;
    const DIE_ORDER = 3;
    const STOP_ORDER = 4;
    const REFUND_ORDER = 5;
    const REFUNDED_ORDER = 6;
    const SERVICE_ORDER = 7;


    protected static $orderStatusArr = [
        self::WAIT_DISTRIBUTE => '未分配',
        self::UNDERWAY => '进行中',
        self::FINALIZE => '已定稿',
        self::DIE_ORDER => '死单',
        self::STOP_ORDER => '暂停',
        self::REFUND_ORDER => '退款中',
        self::REFUNDED_ORDER => '已退款',
        self::SERVICE_ORDER => '售后中',
    ];

    protected static $orderSubStatusArr = [
        1 => '新单',
        2 => '重新设计',
        3 => '大修改',
        4 => '小修改',
        5 => '待跟进'
    ];

    public function getStatusTextAttr($value, $data)
    {
        return self::$orderStatusArr[$data['status']];
    }


    public static function getOrderStatusArr()
    {
        return self::$orderStatusArr;
    }

    public static function getOrderTxt($orderStatus)
    {
        return self::$orderStatusArr[$orderStatus] ?? '';
    }

    public static function getSubStatusTextAttr($orderStatus)
    {
        return self::$orderSubStatusArr[$orderStatus];
    }

    public function user()
    {
        return $this->hasOne(UserModel::class, 'user_id', 'user_id')->bind(['user_name' => 'realname']);
    }

    public function design()
    {
        return $this->hasOne(UserModel::class, 'user_id', 'design_id')->bind(['design_name' => 'realname']);
    }

    public function sales()
    {
        return $this->hasOne(UserModel::class, 'user_id', 'sale_id')->bind(['sale_name' => 'realname']);
    }

    public function designType()
    {
        return $this->hasOne(TypeDesignModel::class, 'id', 'design_type')->bind(['design_type_name' => 'name']);
    }

    /**
     * 交稿时间
     * @param $value
     * @return false|int
     */
    public function setDeliveryTimeAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return strtotime($value);
    }

    /**
     * 交稿时间
     * @param $value
     * @return false|string
     */
    public function getDeliveryTimeAttr($value)
    {
        if (!$value) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }

    public function setDesignNeedFileAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return implode(',', $value);
    }

    public function getDesignNeedFileAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return explode(',', $value);
    }

    public function setReplenishNeedFileAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return implode(',', $value);
    }

    public function getReplenishNeedFileAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return explode(',', $value);
    }


    public function setRemarkFileIdAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return implode(',', $value);
    }

    public function getRemarkFileIdAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return explode(',', $value);
    }

    public function setFollowTimeAttr($value)
    {
        if (!$value) {
            return $value;
        }
        return strtotime($value);
    }

    public function getFollowTimeAttr($value)
    {
        if (!$value) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }

    public function getRefundTimeAttr($value)
    {
        if (!$value) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }

    public function getFinalizeTimeAttr($value)
    {
        if (!$value) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }
}